<template>
	<view class="nearbyset">
		<view class="list" v-for="(value, key, index) in blueObj" @click="connectFun(key)">
			<view class="list_left">
				<view class="title">设备名称</view>
				<view class="text">{{value}}</view>
			</view>
			<view class="list_state" v-show="currentKey == key">{{listStateText}}</view>
		</view>
		<view class="stateText" v-show="blueObj==''">
			{{stateTextObj[state]}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				blueObj:"",
				source:"",
				stateTextObj:{
					'0':"正在扫描附近可识别设备...",
					'1':'未发现蓝牙设备',
					'2':'请先打开蓝牙'
				},
				state:0,
				listStateText:'正在连接中',
				currentKey:''
			}
		},
		onLoad:function(){
			this.source = this.$store.state.global.source;
			
			this.bluetoothGetDevicesFun();//获取蓝牙列表
			// this.blueToothAutoConnectFun();//自动连接最后一次连接设备(功能暂时屏蔽)
		},
		methods: {
			//自动连接逻辑（自动连接最后一次连接设备）
			blueToothAutoConnectFun:function(){
				let that = this;
				uni.showToast({
					icon:'none',
					title:'开始自动连接'
				})
				console.log("自动连接设备")
				
				cm.on('blueToothAutoConnect2',function(res){
					console.log(res)
				})
				cm.blueToothAutoConnect2({
					success:function(res){
						console.log(res)
						if(res == "请先打开蓝牙"){
							uni.showToast({
								icon:'none',
								title:res
							})
						}else{
							let data = JSON.parse(res)
							if(data.code == 3){
								uni.showToast({
									icon:'none',
									title:'蓝牙自动连接成功'
								})
								console.log("蓝牙连接成功")
								uni.redirectTo({
									url:'../nearbysetShop/nearbysetShop'
								})
							}else if(data.code == 2){
								console.log("蓝牙连接中")
							}else if(data.code == 7){
								console.log("正在扫描")
							}else{
								console.log("自动连接失败")
								// uni.showToast({
								// 	icon:'none',
								// 	title:res.message
								// })
								that.bluetoothGetDevicesFun();//获取蓝牙列表
							}
						}
					}
				})
			},
			//获取蓝牙列表逻辑
			bluetoothGetDevicesFun:function(){
				let that = this;
				if(that.source == "andriod"){
					console.log("andriod获取蓝牙列表开始")
					cm.on('bluetoothGetDevices2',function(res){
						console.log(res)
						if(res == "请先打开蓝牙"){
							uni.showToast({
								icon:'none',
								title:res
							})
							return ;
						}
						
						let blueObj = JSON.parse(res)
						console.log(blueObj)
						//判断返回的对象是否为空
						if(Object.keys(blueObj).length == 0){
							that.state = 1;
						}else{
							that.blueObj = blueObj;
						}
					})
					cm.bluetoothGetDevices2({
						success:function(res){
							console.log(res)
						}
					})
				}else if(that.source == "ios"){
					console.log("ios获取蓝牙列表开始")
					cm.on('bluetoothGetDevicesIOS2',function(res){
						console.log(res)
						if(res.code == 4){
							uni.showToast({
								icon:'none',
								title:res.message
							})
							return ;
						}
						
						let blueObj = JSON.parse(res).data
						console.log(blueObj)
						//判断返回的对象是否为空
						if(Object.keys(blueObj).length == 0){
							that.state = 1;
						}else{
							that.blueObj = blueObj;
						}
					})
					cm.bluetoothGetDevicesIOS2({
						success:function(res){
							console.log(res)
						}
					})
				}
			},
			//手动连接
			connectFun:function(identifier){
				let that = this;
				console.log("手动连接")
				console.log(identifier)
				
				this.listStateText = "正在连接中";
				this.currentKey = identifier;
				
				cm.on('blueToothConnect2',function(res){
					console.log(res)
				})
				cm.blueToothConnect2({
					id:identifier,
					success:function(res){
						console.log("手动连接数据返回")
						let data = JSON.parse(res)
						console.log(data)
						if(data.code == 3){ //连接成功
							that.listStateText = "连接成功"
							uni.redirectTo({
								url:'../nearbysetShop/nearbysetShop'
							})
						}else{
							that.listStateText = data.message
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.nearbyset{
		font-size: $uni-font-size-base;
		height: 100%;	
		background-color: #F5F5F5;
		.list{
			display: flex;
			padding: 30rpx;
			border-bottom: 1rpx solid #E5E5E5;
			background-color: #FFFFFF;
			justify-content: space-between;
			.list_left{
				display: flex;
				.title{
					margin-right: 30rpx;
				}
			}
			// .title{
			// 	width: 20%;
			// }
		}
		.stateText{
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #FFFFFF;
		}
	}
</style>
